package com.will.txj.aj.security.config.handle;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.will.txj.aj.security.service.AJUserService;
import com.will.txj.common.security.AJUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author: wen-yi;
 * @date: 2021/12/5 23:09;
 * @Description: 增强链:增强jwt自定义 而jwt是根据oauth2转换而来
 */
@Slf4j
@Component
public class MyTokenEnhancer implements TokenEnhancer {

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) accessToken;
        AJUser principal = (AJUser) authentication.getPrincipal();
        //log.info("增强链: 获取认证的用户信息 --> principal: {}",principal);
        Map<String,Object> map = new HashMap();
        map.put("uId", principal.getId());
        map.put("username", principal.getUsername());
        map.put("nickname", principal.getNickname());
        map.put("sex", principal.getSex());
        map.put("birthday", principal.getBirthday());
        token.setAdditionalInformation(map);
        log.info("写入非敏感数据 —> user: {}",map);
        return token;
    }
}
